using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace BlockOS.Client
{
    public class AddServerDialog : BaseInputActionMapWindow
    {
        public string modifyName = null;
        [SerializeField]
        private TMP_Text title;
        [SerializeField]
        private TMP_InputField[] inputFields;
        private int curInputIdx;

        private TMP_InputField nameInput => inputFields[0];
        private TMP_InputField addrInput => inputFields[1];
        private TMP_InputField portInput => inputFields[2];

        protected override void Awake()
        {
            inputs["Close"].performed += OnCloseAction;
            inputs["NavInput"].performed += OnNavInputAction;
            inputs["SubmitAddr"].performed += OnSubmitAddrAction;

            for (int i = 0; i < inputFields.Length; i++)
            {
                int j = i;
                inputFields[i].onSelect.AddListener(_ =>
                {
                    curInputIdx = j;
                });
            }
        }

        private void OnSubmitAddrAction(UnityEngine.InputSystem.InputAction.CallbackContext obj)
        {
            OnSubmitServerAddress();
        }

        public override void OnShow()
        {
            if (string.IsNullOrEmpty(modifyName))
            {
                nameInput.text = "";
                nameInput.interactable = true;
                addrInput.text = "";
                portInput.text = "";

                title.text = "Add Server";
            }
            else
            {
                var ep = GameClient.Instance.ServerList.GetSever(modifyName);
                nameInput.text = modifyName;
                nameInput.interactable = false;
                addrInput.text = ep.Address.ToString();
                portInput.text = ep.Port.ToString();

                title.text = "Modify";
            }
        }

        public override void OnHide()
        {
            
        }

        protected override void OnDestroy()
        {
            inputs["Close"].performed -= OnCloseAction;
            inputs["NavInput"].performed -= OnNavInputAction;
            inputs["SubmitAddr"].performed -= OnSubmitAddrAction;
        }

        public void OnSubmitServerAddress()
        {
            var nameStr = nameInput.text;
            var addrStr = addrInput.text;
            var portStr = portInput.text;

            if (string.IsNullOrEmpty(addrStr))
            {
                GameClient.Instance.ShowToast("Address can't be empty.");
                return;
            }

            if (string.IsNullOrEmpty(portStr))
            {
                GameClient.Instance.ShowToast($"port can't be empty.");
            }

            var ep = new IPEndPoint(Dns.GetHostAddresses(addrStr)[0], int.Parse(portStr));
            if (string.IsNullOrEmpty(modifyName))
            {
                GameClient.Instance.ShowToast($"End point added: {ep}", 2.0f);
                GameClient.Instance.ServerList.AddServer(nameStr, ep);
            }
            else
            {
                GameClient.Instance.ServerList.SetServer(nameStr, ep);
            }

            GameClient.Instance.EventBus.AddEvent(new GameEvent(ClientEventId.SERVER_LIST_UPDATE));

            modifyName = null;
            
            manager.PopWindow(true);
        }

        private void OnNavInputAction(UnityEngine.InputSystem.InputAction.CallbackContext obj)
        {
            curInputIdx = (curInputIdx + 1) % inputFields.Length;
            for (int i = 0; i < inputFields.Length; i++)
            {
                if (i != curInputIdx) inputFields[i].ReleaseSelection();
            }
            inputFields[curInputIdx].Select();
        }

        private void OnCloseAction(UnityEngine.InputSystem.InputAction.CallbackContext obj)
        {
            manager.PopWindow(true);
        }
    }
}
